package cc.admin.modules.wea.service.impl;

import cc.admin.common.util.MD5Util;
import cc.admin.common.util.RedisUtil;
import cc.admin.common.util.geo.GeometryRelated;
import cc.admin.common.util.geo.GeometryTools;
import cc.admin.modules.wea.entity.WeaPlatform;
import cc.admin.modules.wea.entity.WeaWarningRecord;
import cc.admin.modules.wea.mapper.WeaWarningRecordMapper;
import cc.admin.modules.wea.service.IWeaPlatformService;
import cc.admin.modules.wea.service.IWeaWarningRecordService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 预警历史表
 * @Author: cc-admin
 * @Date:   2024-08-30
 * @Version: V1.0.0
 */
@Service
@Slf4j
public class WeaWarningRecordServiceImpl extends ServiceImpl<WeaWarningRecordMapper, WeaWarningRecord> implements IWeaWarningRecordService {
    private static Map<String, Object> warnType = new HashMap<>();
    private static List<String> typeList = new ArrayList<>();
    static {
        typeList.add("Wave0");
        typeList.add("Wave1");
        typeList.add("Storm0");
        typeList.add("Storm1");
        typeList.add("SeaIce");
        typeList.add("Tsunami");

        warnType.put("Wave0","海浪警报");
        warnType.put("Wave1","海浪警报");
        warnType.put("Storm0","风暴潮警报");
        warnType.put("Storm1","风暴潮警报");
        warnType.put("SeaIce","海冰警报");
        warnType.put("Tsunami","海啸警报");
    }
    @Autowired
    WeaWarningRecordMapper weaWarningRecordMapper;
    @Autowired
    private IWeaPlatformService weaPlatformService;
    @Autowired
    RedisUtil redisUtil;
    @Override
    public IPage<WeaWarningRecord> getPlatName(Page<WeaWarningRecord> page) {
        return weaWarningRecordMapper.getPlatName(page);
    }

    @Override
    public List<WeaWarningRecord> createWeaWarningRecordList(String platformId) {
        List<WeaWarningRecord> list = new ArrayList<>();
        for (String type : typeList){
            if ("Tsunami".equals(type)) {
                for (int i = 0; i < 5;i++){
                    WeaWarningRecord warnRecord  = createWeaWarningRecord(platformId, type + i);
                    if (warnRecord != null) {
                        list.add(warnRecord);
                    }
                }
            }else {
                WeaWarningRecord warnRecord  = createWeaWarningRecord(platformId, type);
                if (warnRecord != null) {
                    list.add(warnRecord);
                }
            }
        }
        this.saveBatch(list);
        return list;
    }

    /**
     * 生成匹配预警记录
     * @param platformId
     * @param type
     * @return
     */
    public WeaWarningRecord createWeaWarningRecord(String platformId,String type) {
        WeaWarningRecord record = null;
        String data  = (String) redisUtil.get(type);

        WeaPlatform platform = weaPlatformService.getById(platformId);

        if(StrUtil.isNotEmpty(data)){

            JSONObject jsonObject = JSONObject.parseObject(data);

            String geom = jsonObject.getString("geom");
            if (isInside(platform,geom,type)){
                record = new WeaWarningRecord();
                record.setPlatformId(platformId);
                record.setWarningDetailId(jsonObject.getString("id"));
                record.setData(data);
                record.setWarningType(type);

                String currentMd5 = MD5Util.MD5Encode(JSONObject.toJSONString(record),"utf-8");
                String storedMd5 = (String)redisUtil.get(type+"RecordMD5"+platformId);
                if (storedMd5 == null || !currentMd5.equals(storedMd5)) {
                    record.setId(IdUtil.fastUUID());
                    record.setCreateTime(new Date());
                    redisUtil.set(type+"RecordMD5"+platformId, currentMd5);
                    redisUtil.set(type+"Record"+platformId, JSONObject.toJSONString(record));

                    log.info("预警数据匹配成功:{}",type+platformId);
                }else {
                    log.info("预警数据重复:{}",type+platformId);
                    record = null;
                }
            }
        }else {
            log.info("预警数据为空:{}",type+platformId);
        }

        return record;
    }

    /**
     * 判断预警数据是否在预警区域
     * @param platform
     * @param geom
     * @param type
     * @return
     */
    private boolean isInside(WeaPlatform platform, String geom,String type)  {
        GeometryRelated gr = new GeometryRelated();
//      海浪警报
      if(type.equals("Wave0")){
        try {
          System.out.println(gr.withinGeo(Double.parseDouble(platform.getLat()),Double.parseDouble(platform.getLon()),geom));
        }catch (Exception  e){
          e.printStackTrace();
        }
      };
      if(type.equals("Wave1")){
        try {
          System.out.println(gr.withinGeo(Double.parseDouble(platform.getLat()),Double.parseDouble(platform.getLon()),geom));
        }catch (Exception  e){
          e.printStackTrace();
        }
      }
      if(type.equals("SeaIce")){

      }

        return true;
    }
}
